// Copyright 2019 The TCMalloc Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "absl/types/span.h"
#include "tcmalloc/common.h"
#include "tcmalloc/internal/config.h"
#include "tcmalloc/size_class_info.h"

GOOGLE_MALLOC_SECTION_BEGIN
namespace tcmalloc {
namespace tcmalloc_internal {

// Columns in the following tables:
// - bytes: size of the size class
// - pages: number of pages per span
// - batch: preferred number of objects for transfers between caches
// - cap: maximum cpu cache capacity
// - class: size class number
// - objs: number of objects per span
// - waste/fixed: fixed per-size-class overhead due to end-of-span fragmentation
//   and other factors. For instance, if we have a 96 byte size class, and use
//   a single 8KiB page, then we will hold 85 objects per span, and have 32
//   bytes left over. There is also a fixed component of 48 bytes of TCMalloc
//   metadata per span. Together, the fixed overhead would be wasted/allocated
//   = (32 + 48) / (8192 - 32) ~= 0.98%.
// - waste/sampling: overhead due to heap sampling
//   (rounding to page size, proxy object, metadata).
// - inc: increment from the previous size class. This caps the dynamic
//   overhead component based on mismatches between the number of bytes
//   requested and the number of bytes provided by the size class. Together
//   they sum to the total overhead; for instance if you asked for a 50-byte
//   allocation that rounds up to a 64-byte size class, the dynamic overhead
//   would be 28%, and if waste were 22% it would mean (on average) 25 bytes
//   of overhead for allocations of that size.

// clang-format off
#if defined(__cpp_aligned_new) && __STDCPP_DEFAULT_NEW_ALIGNMENT__ <= 8
#if TCMALLOC_PAGE_SHIFT == 13
static_assert(kMaxSize == 262144, "kMaxSize mismatch");
static constexpr SizeClassAssumptions Assumptions{
  .has_expanded_classes = true,
  .span_size = 48,
  .sampling_interval = 2097152,
  .large_size = 1024,
  .large_size_alignment = 128,
};
static constexpr SizeClassInfo List[] = {
//                                         |    waste     |
//  bytes pages batch   cap    class  objs |fixed sampling|    inc
  {     0,    0,    0,    0},  //  0     0  0.00%    0.00%   0.00%
  {     8,    1,   32, 5523},  //  0  1024  0.58%    0.42%   0.00%
  {    16,    1,   32, 5523},  //  1   512  0.58%    0.42% 100.00%
  {    32,    1,   32, 5524},  //  2   256  0.58%    0.42% 100.00%
  {    64,    1,   32, 5523},  //  3   128  0.58%    0.42% 100.00%
  {    80,    1,   32, 5523},  //  4   102  0.97%    0.42%  25.00%
  {    96,    1,   32, 4952},  //  5    85  0.97%    0.42%  20.00%
  {   112,    1,   32, 2775},  //  6    73  0.78%    0.42%  16.67%
  {   128,    1,   32, 3153},  //  7    64  0.58%    0.42%  14.29%
  {   144,    1,   32, 1761},  //  8    56  2.14%    0.42%  12.50%
  {   168,    1,   32, 2190},  //  9    48  2.14%    0.42%  16.67%
  {   208,    1,   32, 1909},  // 10    39  1.55%    0.42%  23.81%
  {   256,    1,   32, 2109},  // 11    32  0.58%    0.42%  23.08%
  {   312,    1,   32, 1162},  // 12    26  1.55%    0.42%  21.88%
  {   352,    1,   32,  866},  // 13    23  1.75%    0.42%  12.82%
  {   448,    1,   32,  887},  // 14    18  2.14%    0.42%  27.27%
  {   512,    1,   32,  966},  // 15    16  0.58%    0.42%  14.29%
  {   576,    1,   32,  577},  // 16    14  2.14%    0.42%  12.50%
  {   704,    1,   32,  607},  // 17    11  6.02%    0.43%  22.22%
  {   896,    1,   32,  599},  // 18     9  2.14%    0.43%  27.27%
  {  1024,    1,   32,  804},  // 19     8  0.58%    0.42%  14.29%
  {  1152,    2,   32,  485},  // 20    14  1.85%    0.43%  12.50%
  {  1408,    2,   32,  494},  // 21    11  5.74%    0.43%  22.22%
  {  1792,    2,   32,  485},  // 22     9  1.85%    0.43%  27.27%
  {  2048,    2,   32,  480},  // 23     8  0.29%    0.42%  14.29%
  {  2688,    2,   24,  437},  // 24     6  1.85%    0.43%  31.25%
  {  3456,    3,   18,  402},  // 25     7  1.75%    0.43%  28.57%
  {  4096,    1,   16,  758},  // 26     2  0.58%    0.43%  18.52%
  {  4736,    3,   13,  388},  // 27     5  3.83%    0.43%  15.62%
  {  6144,    3,   10,  378},  // 28     4  0.19%    0.42%  29.73%
  {  8192,    1,    8,  502},  // 29     1  0.58%    0.03%  33.33%
  {  9472,    5,    6,  356},  // 30     4  7.61%    0.85%  15.62%
  { 12288,    3,    5,  364},  // 31     2  0.19%    0.82%  29.73%
  { 16384,    2,    4,  372},  // 32     1  0.29%    0.03%  33.33%
  { 20480,    5,    3,  356},  // 33     2  0.12%    1.21%  25.00%
  { 28672,    7,    2,  356},  // 34     2  0.08%    1.60%  40.00%
  { 32768,    4,    2,  394},  // 35     1  0.15%    0.03%  14.29%
  { 40960,    5,    2,  351},  // 36     1  0.12%    0.03%  25.00%
  { 49152,    6,    2,  348},  // 37     1  0.10%    0.03%  20.00%
  { 65536,    8,    2,  364},  // 38     1  0.07%    0.03%  33.33%
  { 73728,    9,    2,  348},  // 39     1  0.07%    0.03%  12.50%
  { 81920,   10,    2,  348},  // 40     1  0.06%    0.03%  11.11%
  { 98304,   12,    2,  348},  // 41     1  0.05%    0.03%  20.00%
  {114688,   14,    2,  345},  // 42     1  0.04%    0.03%  16.67%
  {131072,   16,    2,  348},  // 43     1  0.04%    0.03%  14.29%
  {155648,   19,    2,  348},  // 44     1  0.03%    0.03%  18.75%
  {204800,   25,    2,  345},  // 45     1  0.02%    0.03%  31.58%
  {262144,   32,    2,  345},  // 46     1  0.02%    0.03%  28.00%
};
#elif TCMALLOC_PAGE_SHIFT == 15
static_assert(kMaxSize == 262144, "kMaxSize mismatch");
static constexpr SizeClassAssumptions Assumptions{
  .has_expanded_classes = true,
  .span_size = 48,
  .sampling_interval = 2097152,
  .large_size = 1024,
  .large_size_alignment = 128,
};
static constexpr SizeClassInfo List[] = {
//                                         |    waste     |
//  bytes pages batch   cap    class  objs |fixed sampling|    inc
  {     0,    0,    0,    0},  //  0     0  0.00%    0.00%   0.00%
  {     8,    1,   32, 5849},  //  0  4096  0.15%    1.60%   0.00%
  {    16,    1,   32, 5849},  //  1  2048  0.15%    1.60% 100.00%
  {    32,    1,   32, 5850},  //  2  1024  0.15%    1.60% 100.00%
  {    64,    1,   32, 5849},  //  3   512  0.15%    1.60% 100.00%
  {    80,    1,   32, 4867},  //  4   409  0.29%    1.60%  25.00%
  {   104,    1,   32, 5849},  //  5   315  0.17%    1.60%  30.00%
  {   128,    1,   32, 4090},  //  6   256  0.15%    1.60%  23.08%
  {   160,    1,   32, 2293},  //  7   204  0.54%    1.60%  25.00%
  {   200,    1,   32, 2336},  //  8   163  0.66%    1.60%  25.00%
  {   256,    1,   32, 2079},  //  9   128  0.15%    1.60%  28.00%
  {   312,    1,   32, 1014},  // 10   105  0.17%    1.60%  21.88%
  {   400,    1,   32, 1194},  // 11    81  1.27%    1.60%  28.21%
  {   512,    1,   32, 1140},  // 12    64  0.15%    1.60%  28.00%
  {   640,    1,   32,  843},  // 13    51  0.54%    1.60%  25.00%
  {   768,    1,   32,  683},  // 14    42  1.71%    1.60%  20.00%
  {   896,    1,   32,  565},  // 15    36  1.71%    1.60%  16.67%
  {  1024,    1,   32,  765},  // 16    32  0.15%    1.60%  14.29%
  {  1280,    1,   32,  568},  // 17    25  2.49%    1.60%  25.00%
  {  1536,    1,   32,  548},  // 18    21  1.71%    1.60%  20.00%
  {  2048,    1,   32,  568},  // 19    16  0.15%    1.60%  33.33%
  {  2176,    1,   30,  454},  // 20    15  0.54%    1.60%   6.25%
  {  2688,    1,   24,  448},  // 21    12  1.71%    1.60%  23.53%
  {  3200,    1,   20,  414},  // 22    10  2.49%    1.60%  19.05%
  {  4096,    1,   16,  714},  // 23     8  0.15%    1.60%  28.00%
  {  4608,    1,   14,  403},  // 24     7  1.71%    1.60%  12.50%
  {  5376,    1,   12,  388},  // 25     6  1.71%    1.60%  16.67%
  {  6528,    1,   10,  408},  // 26     5  0.54%    1.60%  21.43%
  {  8192,    1,    8,  503},  // 27     4  0.15%    1.60%  25.49%
  {  9344,    2,    7,  386},  // 28     7  0.27%    1.60%  14.06%
  { 10880,    1,    6,  374},  // 29     3  0.54%    1.60%  16.44%
  { 13952,    3,    4,  386},  // 30     7  0.70%    1.60%  28.24%
  { 16384,    1,    4,  468},  // 31     2  0.15%    1.60%  17.43%
  { 19072,    3,    3,  377},  // 32     5  3.04%    1.62%  16.41%
  { 24576,    3,    2,  377},  // 33     4  0.05%    1.60%  28.86%
  { 32768,    1,    2,  406},  // 34     1  0.15%    0.03%  33.33%
  { 40960,    4,    2,  371},  // 35     3  6.28%    3.28%  25.00%
  { 49152,    3,    2,  368},  // 36     2  0.05%    3.16%  20.00%
  { 57344,    7,    2,  368},  // 37     4  0.02%    3.16%  16.67%
  { 65536,    2,    2,  386},  // 38     1  0.07%    0.03%  14.29%
  { 81920,    5,    2,  371},  // 39     2  0.03%    4.72%  25.00%
  { 98304,    3,    2,  366},  // 40     1  0.05%    0.03%  20.00%
  {114688,    7,    2,  366},  // 41     2  0.02%    6.28%  16.67%
  {131072,    4,    2,  411},  // 42     1  0.04%    0.03%  14.29%
  {163840,    5,    2,  366},  // 43     1  0.03%    0.03%  25.00%
  {196608,    6,    2,  366},  // 44     1  0.02%    0.03%  20.00%
  {229376,    7,    2,  366},  // 45     1  0.02%    0.03%  16.67%
  {262144,    8,    2,  368},  // 46     1  0.02%    0.03%  14.29%
};
#elif TCMALLOC_PAGE_SHIFT == 18
static_assert(kMaxSize == 262144, "kMaxSize mismatch");
static constexpr SizeClassAssumptions Assumptions{
  .has_expanded_classes = true,
  .span_size = 48,
  .sampling_interval = 2097152,
  .large_size = 1024,
  .large_size_alignment = 128,
};
static constexpr SizeClassInfo List[] = {
//                                         |    waste     |
//  bytes pages batch   cap    class  objs |fixed sampling|    inc
  {     0,    0,    0,    0},  //  0     0  0.00%    0.00%   0.00%
  {     8,    1,   32, 5871},  //  0 32768  0.02%   12.53%   0.00%
  {    16,    1,   32, 5871},  //  1 16384  0.02%   12.53% 100.00%
  {    32,    1,   32, 5871},  //  2  8192  0.02%   12.53% 100.00%
  {    64,    1,   32, 5871},  //  3  4096  0.02%   12.53% 100.00%
  {    80,    1,   32, 5871},  //  4  3276  0.04%   12.53%  25.00%
  {    96,    1,   32, 2938},  //  5  2730  0.04%   12.53%  20.00%
  {   112,    1,   32, 2185},  //  6  2340  0.04%   12.53%  16.67%
  {   128,    1,   32, 1929},  //  7  2048  0.02%   12.53%  14.29%
  {   144,    1,   32, 1786},  //  8  1820  0.04%   12.53%  12.50%
  {   176,    1,   32, 2351},  //  9  1489  0.05%   12.53%  22.22%
  {   208,    1,   32, 1537},  // 10  1260  0.04%   12.53%  18.18%
  {   256,    1,   32, 1935},  // 11  1024  0.02%   12.53%  23.08%
  {   304,    1,   32, 1293},  // 12   862  0.05%   12.53%  18.75%
  {   384,    1,   32, 1218},  // 13   682  0.12%   12.53%  26.32%
  {   448,    1,   32,  894},  // 14   585  0.04%   12.53%  16.67%
  {   512,    1,   32,  780},  // 15   512  0.02%   12.53%  14.29%
  {   576,    1,   32,  570},  // 16   455  0.04%   12.53%  12.50%
  {   704,    1,   32,  774},  // 17   372  0.12%   12.53%  22.22%
  {   896,    1,   32,  677},  // 18   292  0.21%   12.53%  27.27%
  {  1024,    1,   32,  737},  // 19   256  0.02%   12.53%  14.29%
  {  1152,    1,   32,  519},  // 20   227  0.26%   12.53%  12.50%
  {  1408,    1,   32,  536},  // 21   186  0.12%   12.53%  22.22%
  {  1664,    1,   32,  628},  // 22   157  0.36%   12.53%  18.18%
  {  2048,    1,   32,  611},  // 23   128  0.02%   12.53%  23.08%
  {  2304,    1,   28,  464},  // 24   113  0.70%   12.53%  12.50%
  {  2688,    1,   24,  490},  // 25    97  0.56%   12.53%  16.67%
  {  3328,    1,   19,  424},  // 26    78  0.99%   12.54%  23.81%
  {  4096,    1,   16,  708},  // 27    64  0.02%   12.53%  23.08%
  {  4480,    1,   14,  398},  // 28    58  0.90%   12.54%   9.38%
  {  5504,    1,   11,  413},  // 29    47  1.34%   12.54%  22.86%
  {  6528,    1,   10,  387},  // 30    40  0.41%   12.53%  18.60%
  {  8192,    1,    8,  433},  // 31    32  0.02%   12.53%  25.49%
  {  9344,    1,    7,  381},  // 32    28  0.21%   12.53%  14.06%
  { 11392,    1,    5,  378},  // 33    23  0.07%   12.53%  21.92%
  { 13696,    1,    4,  384},  // 34    19  0.75%   12.54%  20.22%
  { 16384,    1,    4,  396},  // 35    16  0.02%   12.53%  19.63%
  { 18688,    1,    3,  376},  // 36    14  0.21%   12.54%  14.06%
  { 21760,    1,    3,  376},  // 37    12  0.41%   12.54%  16.44%
  { 26112,    1,    2,  376},  // 38    10  0.41%   12.54%  20.00%
  { 29056,    1,    2,  373},  // 39     9  0.26%   12.54%  11.27%
  { 32768,    1,    2,  436},  // 40     8  0.02%   12.53%  12.78%
  { 37376,    1,    2,  370},  // 41     7  0.21%   12.54%  14.06%
  { 43648,    1,    2,  370},  // 42     6  0.12%   12.54%  16.78%
  { 52352,    1,    2,  370},  // 43     5  0.16%   12.54%  19.94%
  { 65536,    1,    2,  373},  // 44     4  0.02%   12.53%  25.18%
  { 87296,    1,    2,  370},  // 45     3  0.12%   12.54%  33.20%
  {104832,    2,    2,  367},  // 46     5  0.03%   12.54%  20.09%
  {131072,    1,    2,  370},  // 47     2  0.02%   12.53%  25.03%
  {174720,    2,    2,  366},  // 48     3  0.03%   12.54%  33.30%
  {262144,    1,    2,  370},  // 49     1  0.02%    0.03%  50.04%
};
#elif TCMALLOC_PAGE_SHIFT == 12
static_assert(kMaxSize == 8192, "kMaxSize mismatch");
static constexpr SizeClassAssumptions Assumptions{
  .has_expanded_classes = false,
  .span_size = 48,
  .sampling_interval = 524288,
  .large_size = 1024,
  .large_size_alignment = 128,
};
static constexpr SizeClassInfo List[] = {
//                                         |    waste     |
//  bytes pages batch   cap    class  objs |fixed sampling|    inc
  {     0,    0,    0,    0},  //  0     0  0.00%    0.00%   0.00%
  {     8,    1,   32, 6600},  //  0   512  1.16%    0.92%   0.00%
  {    16,    1,   32, 6600},  //  1   256  1.16%    0.92% 100.00%
  {    32,    1,   32, 6600},  //  2   128  1.16%    0.92% 100.00%
  {    64,    1,   32, 6600},  //  3    64  1.16%    0.92% 100.00%
  {    80,    1,   32, 6600},  //  4    51  1.54%    0.92%  25.00%
  {    96,    1,   32, 4080},  //  5    42  2.70%    0.92%  20.00%
  {   112,    1,   32, 2765},  //  6    36  2.70%    0.92%  16.67%
  {   128,    1,   32, 2994},  //  7    32  1.16%    0.92%  14.29%
  {   144,    1,   32, 2272},  //  8    28  2.70%    0.92%  12.50%
  {   192,    1,   32, 3623},  //  9    21  2.70%    0.92%  33.33%
  {   256,    1,   32, 2949},  // 10    16  1.16%    0.92%  33.33%
  {   288,    1,   32,  764},  // 11    14  2.70%    0.92%  12.50%
  {   368,    1,   32, 1163},  // 12    11  2.32%    0.92%  27.78%
  {   512,    1,   32, 1734},  // 13     8  1.16%    0.92%  39.13%
  {   576,    2,   32,  719},  // 14    14  2.14%    0.92%  12.50%
  {   768,    2,   32,  587},  // 15    10  6.80%    0.93%  33.33%
  {   896,    2,   32,  761},  // 16     9  2.14%    0.92%  16.67%
  {  1024,    2,   32, 3507},  // 17     8  0.58%    0.92%  14.29%
  {  1536,    3,   32,  532},  // 18     8  0.39%    0.92%  50.00%
  {  2048,    4,   32,  632},  // 19     8  0.29%    0.92%  33.33%
  {  2688,    4,   24,  413},  // 20     6  1.85%    0.93%  31.25%
  {  3584,    7,   18,  435},  // 21     8  0.17%    0.92%  33.33%
  {  4096,    4,   16, 1573},  // 22     4  0.29%    0.92%  14.29%
  {  6144,    3,   10,  451},  // 23     2  0.39%    1.70%  50.00%
  {  8192,    4,    8,  532},  // 24     2  0.29%    1.70%  33.33%
};
#else
#error "Unsupported TCMALLOC_PAGE_SHIFT value!"
#endif
#else
#if TCMALLOC_PAGE_SHIFT == 13
static_assert(kMaxSize == 262144, "kMaxSize mismatch");
static constexpr SizeClassAssumptions Assumptions{
  .has_expanded_classes = true,
  .span_size = 48,
  .sampling_interval = 2097152,
  .large_size = 1024,
  .large_size_alignment = 128,
};
static constexpr SizeClassInfo List[] = {
//                                         |    waste     |
//  bytes pages batch   cap    class  objs |fixed sampling|    inc
  {     0,    0,    0,    0},  //  0     0  0.00%    0.00%   0.00%
  {     8,    1,   32, 5525},  //  0  1024  0.58%    0.42%   0.00%
  {    16,    1,   32, 5525},  //  1   512  0.58%    0.42% 100.00%
  {    32,    1,   32, 5525},  //  2   256  0.58%    0.42% 100.00%
  {    64,    1,   32, 5525},  //  3   128  0.58%    0.42% 100.00%
  {    80,    1,   32, 5525},  //  4   102  0.97%    0.42%  25.00%
  {    96,    1,   32, 4953},  //  5    85  0.97%    0.42%  20.00%
  {   112,    1,   32, 2776},  //  6    73  0.78%    0.42%  16.67%
  {   128,    1,   32, 3153},  //  7    64  0.58%    0.42%  14.29%
  {   160,    1,   32, 2889},  //  8    51  0.97%    0.42%  25.00%
  {   176,    1,   32, 1346},  //  9    46  1.75%    0.42%  10.00%
  {   208,    1,   32, 1613},  // 10    39  1.55%    0.42%  18.18%
  {   256,    1,   32, 2109},  // 11    32  0.58%    0.42%  23.08%
  {   288,    1,   32,  858},  // 12    28  2.14%    0.42%  12.50%
  {   352,    1,   32, 1171},  // 13    23  1.75%    0.42%  22.22%
  {   448,    1,   32,  887},  // 14    18  2.14%    0.42%  27.27%
  {   512,    1,   32,  966},  // 15    16  0.58%    0.42%  14.29%
  {   576,    1,   32,  577},  // 16    14  2.14%    0.42%  12.50%
  {   704,    1,   32,  607},  // 17    11  6.02%    0.43%  22.22%
  {   896,    1,   32,  599},  // 18     9  2.14%    0.43%  27.27%
  {  1024,    1,   32,  804},  // 19     8  0.58%    0.42%  14.29%
  {  1152,    2,   32,  486},  // 20    14  1.85%    0.43%  12.50%
  {  1408,    2,   32,  494},  // 21    11  5.74%    0.43%  22.22%
  {  1792,    2,   32,  486},  // 22     9  1.85%    0.43%  27.27%
  {  2048,    2,   32,  480},  // 23     8  0.29%    0.42%  14.29%
  {  2688,    2,   24,  437},  // 24     6  1.85%    0.43%  31.25%
  {  3456,    3,   18,  402},  // 25     7  1.75%    0.43%  28.57%
  {  4096,    1,   16,  758},  // 26     2  0.58%    0.43%  18.52%
  {  4736,    3,   13,  388},  // 27     5  3.83%    0.43%  15.62%
  {  6144,    3,   10,  378},  // 28     4  0.19%    0.42%  29.73%
  {  8192,    1,    8,  502},  // 29     1  0.58%    0.03%  33.33%
  {  9472,    5,    6,  356},  // 30     4  7.61%    0.85%  15.62%
  { 12288,    3,    5,  364},  // 31     2  0.19%    0.82%  29.73%
  { 16384,    2,    4,  372},  // 32     1  0.29%    0.03%  33.33%
  { 20480,    5,    3,  356},  // 33     2  0.12%    1.21%  25.00%
  { 28672,    7,    2,  356},  // 34     2  0.08%    1.60%  40.00%
  { 32768,    4,    2,  394},  // 35     1  0.15%    0.03%  14.29%
  { 40960,    5,    2,  351},  // 36     1  0.12%    0.03%  25.00%
  { 49152,    6,    2,  348},  // 37     1  0.10%    0.03%  20.00%
  { 65536,    8,    2,  364},  // 38     1  0.07%    0.03%  33.33%
  { 73728,    9,    2,  348},  // 39     1  0.07%    0.03%  12.50%
  { 81920,   10,    2,  348},  // 40     1  0.06%    0.03%  11.11%
  { 98304,   12,    2,  348},  // 41     1  0.05%    0.03%  20.00%
  {114688,   14,    2,  345},  // 42     1  0.04%    0.03%  16.67%
  {131072,   16,    2,  348},  // 43     1  0.04%    0.03%  14.29%
  {155648,   19,    2,  348},  // 44     1  0.03%    0.03%  18.75%
  {204800,   25,    2,  343},  // 45     1  0.02%    0.03%  31.58%
  {262144,   32,    2,  345},  // 46     1  0.02%    0.03%  28.00%
};
#elif TCMALLOC_PAGE_SHIFT == 15
static_assert(kMaxSize == 262144, "kMaxSize mismatch");
static constexpr SizeClassAssumptions Assumptions{
  .has_expanded_classes = true,
  .span_size = 48,
  .sampling_interval = 2097152,
  .large_size = 1024,
  .large_size_alignment = 128,
};
static constexpr SizeClassInfo List[] = {
//                                         |    waste     |
//  bytes pages batch   cap    class  objs |fixed sampling|    inc
  {     0,    0,    0,    0},  //  0     0  0.00%    0.00%   0.00%
  {     8,    1,   32, 5359},  //  0  4096  0.15%    1.60%   0.00%
  {    16,    1,   32, 5359},  //  1  2048  0.15%    1.60% 100.00%
  {    32,    1,   32, 5359},  //  2  1024  0.15%    1.60% 100.00%
  {    64,    1,   32, 5359},  //  3   512  0.15%    1.60% 100.00%
  {    80,    1,   32, 5359},  //  4   409  0.29%    1.60%  25.00%
  {    96,    1,   32, 4671},  //  5   341  0.24%    1.60%  20.00%
  {   112,    1,   32, 3101},  //  6   292  0.34%    1.60%  16.67%
  {   128,    1,   32, 3590},  //  7   256  0.15%    1.60%  14.29%
  {   160,    1,   32, 2486},  //  8   204  0.54%    1.60%  25.00%
  {   192,    1,   32, 2114},  //  9   170  0.54%    1.60%  20.00%
  {   256,    1,   32, 2666},  // 10   128  0.15%    1.60%  33.33%
  {   320,    1,   32, 1133},  // 11   102  0.54%    1.60%  25.00%
  {   400,    1,   32, 1188},  // 12    81  1.27%    1.60%  25.00%
  {   512,    1,   32, 1201},  // 13    64  0.15%    1.60%  28.00%
  {   640,    1,   32,  869},  // 14    51  0.54%    1.60%  25.00%
  {   768,    1,   32,  691},  // 15    42  1.71%    1.60%  20.00%
  {   896,    1,   32,  560},  // 16    36  1.71%    1.60%  16.67%
  {  1024,    1,   32,  780},  // 17    32  0.15%    1.60%  14.29%
  {  1280,    1,   32,  563},  // 18    25  2.49%    1.60%  25.00%
  {  1536,    1,   32,  539},  // 19    21  1.71%    1.60%  20.00%
  {  2048,    1,   32,  560},  // 20    16  0.15%    1.60%  33.33%
  {  2176,    1,   30,  434},  // 21    15  0.54%    1.60%   6.25%
  {  2688,    1,   24,  426},  // 22    12  1.71%    1.60%  23.53%
  {  3200,    1,   20,  387},  // 23    10  2.49%    1.60%  19.05%
  {  4096,    1,   16,  722},  // 24     8  0.15%    1.60%  28.00%
  {  4608,    1,   14,  377},  // 25     7  1.71%    1.60%  12.50%
  {  5376,    1,   12,  361},  // 26     6  1.71%    1.60%  16.67%
  {  6528,    1,   10,  382},  // 27     5  0.54%    1.60%  21.43%
  {  8192,    1,    8,  487},  // 28     4  0.15%    1.60%  25.49%
  {  9344,    2,    7,  356},  // 29     7  0.27%    1.60%  14.06%
  { 10880,    1,    6,  343},  // 30     3  0.54%    1.60%  16.44%
  { 13952,    3,    4,  358},  // 31     7  0.70%    1.60%  28.24%
  { 16384,    1,    4,  450},  // 32     2  0.15%    1.60%  17.43%
  { 19072,    3,    3,  348},  // 33     5  3.04%    1.62%  16.41%
  { 24576,    3,    2,  345},  // 34     4  0.05%    1.60%  28.86%
  { 32768,    1,    2,  382},  // 35     1  0.15%    0.03%  33.33%
  { 40960,    4,    2,  340},  // 36     3  6.28%    3.28%  25.00%
  { 49152,    3,    2,  338},  // 37     2  0.05%    3.16%  20.00%
  { 57344,    7,    2,  338},  // 38     4  0.02%    3.16%  16.67%
  { 65536,    2,    2,  356},  // 39     1  0.07%    0.03%  14.29%
  { 81920,    5,    2,  340},  // 40     2  0.03%    4.72%  25.00%
  { 98304,    3,    2,  335},  // 41     1  0.05%    0.03%  20.00%
  {114688,    7,    2,  338},  // 42     2  0.02%    6.28%  16.67%
  {131072,    4,    2,  387},  // 43     1  0.04%    0.03%  14.29%
  {163840,    5,    2,  335},  // 44     1  0.03%    0.03%  25.00%
  {196608,    6,    2,  331},  // 45     1  0.02%    0.03%  20.00%
  {229376,    7,    2,  335},  // 46     1  0.02%    0.03%  16.67%
  {262144,    8,    2,  338},  // 47     1  0.02%    0.03%  14.29%
};
#elif TCMALLOC_PAGE_SHIFT == 18
static_assert(kMaxSize == 262144, "kMaxSize mismatch");
static constexpr SizeClassAssumptions Assumptions{
  .has_expanded_classes = true,
  .span_size = 48,
  .sampling_interval = 2097152,
  .large_size = 1024,
  .large_size_alignment = 128,
};
static constexpr SizeClassInfo List[] = {
//                                         |    waste     |
//  bytes pages batch   cap    class  objs |fixed sampling|    inc
  {     0,    0,    0,    0},  //  0     0  0.00%    0.00%   0.00%
  {     8,    1,   32, 5871},  //  0 32768  0.02%   12.53%   0.00%
  {    16,    1,   32, 5871},  //  1 16384  0.02%   12.53% 100.00%
  {    32,    1,   32, 5871},  //  2  8192  0.02%   12.53% 100.00%
  {    64,    1,   32, 5871},  //  3  4096  0.02%   12.53% 100.00%
  {    80,    1,   32, 5871},  //  4  3276  0.04%   12.53%  25.00%
  {    96,    1,   32, 2938},  //  5  2730  0.04%   12.53%  20.00%
  {   112,    1,   32, 2185},  //  6  2340  0.04%   12.53%  16.67%
  {   128,    1,   32, 1929},  //  7  2048  0.02%   12.53%  14.29%
  {   144,    1,   32, 1786},  //  8  1820  0.04%   12.53%  12.50%
  {   176,    1,   32, 2351},  //  9  1489  0.05%   12.53%  22.22%
  {   208,    1,   32, 1537},  // 10  1260  0.04%   12.53%  18.18%
  {   256,    1,   32, 1935},  // 11  1024  0.02%   12.53%  23.08%
  {   304,    1,   32, 1293},  // 12   862  0.05%   12.53%  18.75%
  {   384,    1,   32, 1218},  // 13   682  0.12%   12.53%  26.32%
  {   448,    1,   32,  894},  // 14   585  0.04%   12.53%  16.67%
  {   512,    1,   32,  780},  // 15   512  0.02%   12.53%  14.29%
  {   576,    1,   32,  570},  // 16   455  0.04%   12.53%  12.50%
  {   704,    1,   32,  774},  // 17   372  0.12%   12.53%  22.22%
  {   896,    1,   32,  677},  // 18   292  0.21%   12.53%  27.27%
  {  1024,    1,   32,  737},  // 19   256  0.02%   12.53%  14.29%
  {  1152,    1,   32,  519},  // 20   227  0.26%   12.53%  12.50%
  {  1408,    1,   32,  536},  // 21   186  0.12%   12.53%  22.22%
  {  1664,    1,   32,  628},  // 22   157  0.36%   12.53%  18.18%
  {  2048,    1,   32,  611},  // 23   128  0.02%   12.53%  23.08%
  {  2304,    1,   28,  464},  // 24   113  0.70%   12.53%  12.50%
  {  2688,    1,   24,  490},  // 25    97  0.56%   12.53%  16.67%
  {  3328,    1,   19,  424},  // 26    78  0.99%   12.54%  23.81%
  {  4096,    1,   16,  708},  // 27    64  0.02%   12.53%  23.08%
  {  4480,    1,   14,  398},  // 28    58  0.90%   12.54%   9.38%
  {  5504,    1,   11,  413},  // 29    47  1.34%   12.54%  22.86%
  {  6528,    1,   10,  387},  // 30    40  0.41%   12.53%  18.60%
  {  8192,    1,    8,  433},  // 31    32  0.02%   12.53%  25.49%
  {  9344,    1,    7,  381},  // 32    28  0.21%   12.53%  14.06%
  { 11392,    1,    5,  378},  // 33    23  0.07%   12.53%  21.92%
  { 13696,    1,    4,  384},  // 34    19  0.75%   12.54%  20.22%
  { 16384,    1,    4,  396},  // 35    16  0.02%   12.53%  19.63%
  { 18688,    1,    3,  376},  // 36    14  0.21%   12.54%  14.06%
  { 21760,    1,    3,  376},  // 37    12  0.41%   12.54%  16.44%
  { 26112,    1,    2,  376},  // 38    10  0.41%   12.54%  20.00%
  { 29056,    1,    2,  373},  // 39     9  0.26%   12.54%  11.27%
  { 32768,    1,    2,  436},  // 40     8  0.02%   12.53%  12.78%
  { 37376,    1,    2,  370},  // 41     7  0.21%   12.54%  14.06%
  { 43648,    1,    2,  370},  // 42     6  0.12%   12.54%  16.78%
  { 52352,    1,    2,  370},  // 43     5  0.16%   12.54%  19.94%
  { 65536,    1,    2,  373},  // 44     4  0.02%   12.53%  25.18%
  { 87296,    1,    2,  370},  // 45     3  0.12%   12.54%  33.20%
  {104832,    2,    2,  367},  // 46     5  0.03%   12.54%  20.09%
  {131072,    1,    2,  370},  // 47     2  0.02%   12.53%  25.03%
  {174720,    2,    2,  366},  // 48     3  0.03%   12.54%  33.30%
  {262144,    1,    2,  370},  // 49     1  0.02%    0.03%  50.04%
};
#elif TCMALLOC_PAGE_SHIFT == 12
static_assert(kMaxSize == 8192, "kMaxSize mismatch");
static constexpr SizeClassAssumptions Assumptions{
  .has_expanded_classes = false,
  .span_size = 48,
  .sampling_interval = 524288,
  .large_size = 1024,
  .large_size_alignment = 128,
};
static constexpr SizeClassInfo List[] = {
//                                         |    waste     |
//  bytes pages batch   cap    class  objs |fixed sampling|    inc
  {     0,    0,    0,    0},  //  0     0  0.00%    0.00%   0.00%
  {     8,    1,   32, 6600},  //  0   512  1.16%    0.92%   0.00%
  {    16,    1,   32, 6600},  //  1   256  1.16%    0.92% 100.00%
  {    32,    1,   32, 6600},  //  2   128  1.16%    0.92% 100.00%
  {    64,    1,   32, 6600},  //  3    64  1.16%    0.92% 100.00%
  {    80,    1,   32, 6600},  //  4    51  1.54%    0.92%  25.00%
  {    96,    1,   32, 4080},  //  5    42  2.70%    0.92%  20.00%
  {   112,    1,   32, 2765},  //  6    36  2.70%    0.92%  16.67%
  {   128,    1,   32, 2994},  //  7    32  1.16%    0.92%  14.29%
  {   144,    1,   32, 2272},  //  8    28  2.70%    0.92%  12.50%
  {   192,    1,   32, 3623},  //  9    21  2.70%    0.92%  33.33%
  {   256,    1,   32, 2949},  // 10    16  1.16%    0.92%  33.33%
  {   288,    1,   32,  764},  // 11    14  2.70%    0.92%  12.50%
  {   368,    1,   32, 1163},  // 12    11  2.32%    0.92%  27.78%
  {   512,    1,   32, 1734},  // 13     8  1.16%    0.92%  39.13%
  {   576,    2,   32,  719},  // 14    14  2.14%    0.92%  12.50%
  {   768,    2,   32,  587},  // 15    10  6.80%    0.93%  33.33%
  {   896,    2,   32,  761},  // 16     9  2.14%    0.92%  16.67%
  {  1024,    2,   32, 3507},  // 17     8  0.58%    0.92%  14.29%
  {  1536,    3,   32,  532},  // 18     8  0.39%    0.92%  50.00%
  {  2048,    4,   32,  632},  // 19     8  0.29%    0.92%  33.33%
  {  2688,    4,   24,  413},  // 20     6  1.85%    0.93%  31.25%
  {  3584,    7,   18,  435},  // 21     8  0.17%    0.92%  33.33%
  {  4096,    4,   16, 1573},  // 22     4  0.29%    0.92%  14.29%
  {  6144,    3,   10,  451},  // 23     2  0.39%    1.70%  50.00%
  {  8192,    4,    8,  532},  // 24     2  0.29%    1.70%  33.33%
};
#else
#error "Unsupported TCMALLOC_PAGE_SHIFT value!"
#endif
#endif
// clang-format on

static_assert(sizeof(List) / sizeof(List[0]) <= kNumBaseClasses);
extern constexpr SizeClasses kReuseSizeClasses{List, Assumptions};

}  // namespace tcmalloc_internal
}  // namespace tcmalloc
GOOGLE_MALLOC_SECTION_END
